package com.th.supcom.pds.sortingdrug.client.widget;

import java.util.ArrayList;
import java.util.List;

import net.carefx.fc.framework.web.gwt.util.FcUtils;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.SpinnerField;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.th.supcom.pds.common.client.commom.PdsAuditPrescServiceFactory;
import com.th.supcom.pds.common.enums.DispenseIndicator;
import com.th.supcom.pds.common.enums.SortingIdentification;
import com.th.supcom.pds.sortingdrug.client.model.PdsSupplyProModel;
import com.th.supcom.pds.sortingdrug.client.service.SortingDrugRpcAsync;

/**
 * 记录分拣工作人员工作量窗口
 * 
 * @author 杨冲
 */
public class SortingRecordWindow extends Window
{

    private TextField <String> m_empId;
    private TextField <String> m_empName;
    private SpinnerField m_presNumber;
    private Button m_confirmBtn;
    private Button m_cancelBtn;
    private List <PdsSupplyProModel> m_models;
    private SortingDrugMainPanel m_mainPanel;
    private static final SortingDrugRpcAsync s_DRUG_ASYNC = PdsAuditPrescServiceFactory.getSortingDrugService ();

    public SortingRecordWindow (List <PdsSupplyProModel> models)
    {
        m_models = models;
        initUI ();
        addListeners ();
    }

    public void setMainPanel (SortingDrugMainPanel mainPanel)
    {
    
        m_mainPanel = mainPanel;
    }



    private void initUI ()
    {
        setPixelSize (350, 200);
        setModal (true);
        setBlinkModal (true);
        setHeading ("后台分拣");
        setLayout (new FitLayout ());
        setButtonAlign (HorizontalAlignment.CENTER);
        FormPanel formPanel = new FormPanel ();
        FormLayout layout = new FormLayout ();
        layout.setLabelWidth (60);
        formPanel.setLayout (layout);
        formPanel.setHeaderVisible (false);
        formPanel.setFrame (true);

        m_empId = new TextField <String> ();
        m_empId.setFieldLabel ("员工&nbsp;&nbsp;&nbsp;&nbsp;ID");
        m_empId.focus ();

        m_empName = new TextField <String> ();
        m_empName.setFieldLabel ("姓&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;名");

        m_presNumber = new SpinnerField ();
        m_presNumber.setIncrement (1);
        m_presNumber.getPropertyEditor ().setType (Integer.class);
        m_presNumber.getPropertyEditor ().setFormat (NumberFormat.getFormat ("0"));
        m_presNumber.setFieldLabel ("摆药张数");
        m_presNumber.setMinValue (1);
        m_presNumber.setValue (1);
        m_presNumber.setMaxValue (20);

        m_confirmBtn = new Button ("确定");
        m_cancelBtn = new Button ("取消");

        formPanel.add (m_empId);
        formPanel.add (m_empName);
        formPanel.add (m_presNumber);
        add (formPanel);
        addButton (m_confirmBtn);
        addButton (m_cancelBtn);
    }

    /**
     * 增加监听
     */
    private void addListeners ()
    {
        m_confirmBtn.addSelectionListener (new SelectionListener <ButtonEvent> ()
        {

            @Override
            public void componentSelected (ButtonEvent ce)
            {
                if (!check ())
                {
                    return;
                }

                if (m_presNumber.getValue ().intValue () > m_models.size ())
                {
                    MessageBox.alert ("提示", "没有足够的处方需要分拣", null);
                    return;
                }

                sortPresc (getCurrentPrescNeeded (m_presNumber.getValue ().intValue ()));
            }
        });

        m_cancelBtn.addSelectionListener (new SelectionListener <ButtonEvent> ()
        {

            @Override
            public void componentSelected (ButtonEvent ce)
            {
                hide ();
            }
        });
    }

    private boolean check ()
    {
        if (null == m_empId.getValue ())
        {
            MessageBox.alert ("提示", "员工ID为空", null);
            return false;
        }

        if (null == m_empName.getValue ())
        {
            MessageBox.alert ("提示", "员工姓名为空", null);
            return false;
        }

        return true;
    }

    /**
     * 获取本次需要分拣的处方列表
     * 
     * @return
     */
    private List <PdsSupplyProModel> getCurrentPrescNeeded (int size)
    {
        List <PdsSupplyProModel> list = new ArrayList <PdsSupplyProModel> ();
        for (int i = 0; i < size; i++)
        {
            PdsSupplyProModel model = m_models.get (i);
            model.setSortingDatetime (FcUtils.getCurrentServerDateTimeForClient ());
            model.setSortingIdentification (SortingIdentification.DisSorting.getEnumCode ());
            model.setUserIdSortingPharmacist (m_empId.getValue ());
            model.setDispenseIndicator (DispenseIndicator.Sorted.getEnumCode ());
            model.setUserNameSortingPharmacist (m_empName.getValue ());
            list.add (model);
        }
        return list;
    }

    /**
     * 处方分拣
     */
    private void sortPresc (final List <PdsSupplyProModel> models)
    {
        s_DRUG_ASYNC.modifyPrescDetailByList (models, new AsyncCallback <Void> ()
        {

            @Override
            public void onSuccess (Void result)
            {
                
                Info.display ("提示", "分拣成功");
                hide ();
                m_mainPanel.initData ();
            }

            @Override
            public void onFailure (Throwable caught)
            {
                MessageBox.alert ("提示", caught.getLocalizedMessage (), null);
            }
        });
    }

    /** 提交路径. */
    public static final String SCC_BRANCH = "$URL$";

    /** 提交日期. */
    public static final String SCC_LAST_MODIFICATION_DATE = "$Date$";

    /** 最终修改人员. */
    public static final String SCC_LAST_MODIFIER_NAME = "$Author$";

    /** 最终版本号. */
    public static final String SCC_REVISION = "$Revision$";
}
